#pragma once

#include <string>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/aes.h>
#include <openssl/md5.h>

const int AES_KEY_LENGTH = 16;
#define XDIGIT_TO_NUM(h) ((h) < 'a' ? (h) - '0' : tolower(h) - 'a' + 10)
#define X2DIGITS_TO_NUM(h1, h2) ((XDIGIT_TO_NUM(h1) << 4) + XDIGIT_TO_NUM(h2))

class Utils
{
public:
	static std::string Base64Encode(const char *buffer, int length, bool newLine = false);
	static std::string Base64Decode(const char *input, int length, bool newLine = false);

	// AES录脫脙脺pkcs#5脤卯鲁盲
	static bool AESEncrypt(const char *pszKey, const char *buffer, size_t length, std::string *res);
	// AES陆芒脙脺pkcs#5脤卯鲁盲
	static bool AESDecrypt(const char *pszKey, const char *buffer, size_t length, std::string *res);

	static bool Decode(const char *pszKey, const char *buffer, size_t length, std::string *res);
	static bool Encode(const char *pszKey, const char *buffer, size_t length, std::string *res);

	static std::string MD5(const std::string &src);
	static std::string StrToHex(const unsigned char *p, int iLen);
	static void HexToStr(const unsigned char *pHex, unsigned int nHexLen, unsigned char *pBuf, unsigned int &nBufLen);

	static void MD5_2(const std::string &src, unsigned char *md);
	static bool Encode_2(unsigned char cKeyArr[16], const char *buffer, size_t length, std::string *res);
	static bool AESEncrypt_2(unsigned char cKeyArr[16], const char *buffer, size_t length, std::string *res);
};
